package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.Category;
import com.ruoyi.business.domain.Depart;
import com.ruoyi.business.domain.DepartVO;
import com.ruoyi.business.mapper.CategoryMapper;
import com.ruoyi.business.mapper.DepartMapper;
import com.ruoyi.business.service.ICategoryService;
import com.ruoyi.business.service.IDepartService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdWorker;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CategoryService业务层处理
 *
 * @author win
 * @date 2021-06-14
 */
@Service
public class DepartServiceImpl implements IDepartService
{
    @Autowired
    private DepartMapper departMapper;

    @Resource
    private IdWorker idWorker;

    @Resource
    private ISysDictDataService dictDataService;

    /**
     * @return Category
     */
    @Override
    public Depart selectDepartById(Long id)
    {
        return departMapper.selectDepartById(id);
    }

    /**
     * @return Category
     */
    @Override
    public List<Depart> selectDepartList(Depart depart)
    {
        return departMapper.selectDepartList(depart);
    }

    /**
     * @return 结果
     */
    @Override
    public int insertDepart(Depart depart)
    {
        depart.setId(idWorker.nextId());
        depart.setCreateTime(DateUtils.getNowDate());
        depart.setCreateBy(SecurityUtils.getUsername());
        return departMapper.insertDepart(depart);
    }

    /***
     * @return 结果
     */
    @Override
    public int updateDepart(Depart depart)
    {
        depart.setUpdateTime(DateUtils.getNowDate());
        depart.setUpdateBy(SecurityUtils.getUsername());
        return departMapper.updateDepart(depart);
    }

    /**
     * @return 结果
     */
    @Override
    public int deleteDepartByIds(Long[] ids)
    {
        return departMapper.deleteDepartByIds(ids);
    }

    /***
     * @return 结果
     */
    @Override
    public int deleteDepartById(Long id)
    {
        return departMapper.deleteDepartById(id);
    }

    @Override
    public List<Depart> getDepartByBelong(Long belong) {
        return departMapper.selectDepartList(new Depart().setBelong(belong));
    }

    @Override
    public List<Depart> selectDepartListWithLimit(Depart depart, Integer limit) {
        return departMapper.selectDepartListWithLimit(depart, limit);
    }

    @Override
    public List<DepartVO> selectDepartTree(){
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("business_belong");
        List<SysDictData> belongList = dictDataService.selectDictDataList(sysDictData);

        List<Depart> departs = departMapper.selectDepartList(new Depart().setStatus(1));

        return belongList.stream().map(item -> {
            DepartVO departVO = new DepartVO();
            departVO.setBelong(item.getDictCode());
            departVO.setBelongName(item.getDictLabel());

            List<Depart> target =
                    departs.stream().filter(depart -> String.valueOf(depart.getBelong()).equals(item.getDictValue())).collect(Collectors.toList());
            departVO.setDepartList(target);
            return departVO;
        }).collect(Collectors.toList());
    }
}
